using System;
using System.Collections.Generic;
using System.Text;

using Csla;
using Csla.Data;
using System.Data;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;

namespace buildingLib.xcategory
{
    [Serializable()]
    public class XItemIODetail:
            BusinessListBase<XItemIODetail, XIOItem>
    {
        #region Authorization Rules

        public static bool CanAddObject()
        {
            // TODO: customize to check user role
            //return ApplicationContext.User.IsInRole("");
            return true;
        }

        public static bool CanGetObject()
        {
            // TODO: customize to check user role
            //return ApplicationContext.User.IsInRole("");
            return true;
        }

        public static bool CanEditObject()
        {
            // TODO: customize to check user role
            //return ApplicationContext.User.IsInRole("");
            return true;
        }

        public static bool CanDeleteObject()
        {
            // TODO: customize to check user role
            //return ApplicationContext.User.IsInRole("");
            return true;
        }

        #endregion

 

        #region Factory Methods

        public static XItemIODetail GetIOItemDetail(Guid itemID)
        {
            return DataPortal.Fetch<XItemIODetail>(new Criteria(itemID));
        }

       

        private XItemIODetail()
        {            
        }

        private XItemIODetail(Guid id)
        {            
        }

        #endregion

        #region Data Access

        [Serializable()]
        private class Criteria
        {
            private Guid m_itemID;
            public Guid ItemID
            {
                get { return m_itemID; }
            }
            public Criteria(Guid itemID)
            { m_itemID = itemID; }                        
        }


        private void DataPortal_Fetch(Criteria criteria)
        {
            RaiseListChangedEvents = false;
            Database db = SQLDatabase.GetDatabase();
            DbCommand dbcom = db.GetSqlStringCommand(string.Format("SELECT   x_iolog.id, x_iolog.itemid, x_iolog.ticketid, x_iolog.unit, x_iolog.price, x_iolog.count, x_iolog.type,x_iolog.memo, x_ticket.regDate, x_ticket.owner, x_ticket.department FROM      x_iolog INNER JOIN x_ticket ON x_iolog.ticketid = x_ticket.id where itemid = '{0}' ORDER BY x_ticket.regDate DESC", criteria.ItemID));
            

            using (SafeDataReader dr = new SafeDataReader(db.ExecuteReader(dbcom)))
            {
                while (dr.Read())
                {
                    this.Add(XIOItem.GetXIOItem(dr,true));
                }
            }
            RaiseListChangedEvents = true;
        }

        protected override void DataPortal_Update()
        {
            RaiseListChangedEvents = false;
            foreach (XIOItem item in DeletedList)
                item.DeleteSelf();
            DeletedList.Clear();

            foreach (XIOItem item in this)
                if (item.IsNew)
                    item.Insert();
                else
                    item.Update();
            RaiseListChangedEvents = true;
        }
       

        #endregion
    }
}
